package com.xu.book.dao.impl;

import com.xu.book.bean.Book;
import com.xu.book.dao.IBookDao;
import com.xu.sys.utils.MyDbutils;
import com.xu.sys.utils.PageUtils;
import com.xu.sys.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BookDaoimpl implements IBookDao {

    @Override
    public List<Book> listPage(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_books limit ?,?";
        if(StringUtils.isNotEmpty(pageUtils.getKey())){
            sql = "select * from t_books where name like '%"+pageUtils.getKey()+"%' limit ?,? ";
        }
        // 计算 分页开始的位置
        int startNo = pageUtils.getStart();
        try {
            return queryRunner.query(sql, new BeanListHandler<Book>(Book.class),startNo,pageUtils.getPageSize());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int count(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select count(1) from t_books ";
        if(StringUtils.isNotEmpty(pageUtils.getKey())){
            sql = "select count(1) from t_books where name like '%"+pageUtils.getKey()+"%'  ";
        }
        try {
            return queryRunner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    return resultSet.getInt(1);
                }
            });
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Book> list(Book book) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_books where 1 = 1";
        if (book != null){
            if (book.getTypeid() != null && book.getTypeid() > 0){
                sql += " and typeid = "+ book.getTypeid();
            }
        }
        try {
            return queryRunner.query(sql, new BeanListHandler<Book>(Book.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int save(Book entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "insert into t_books(name,notes,author,publish,img,state,isbn,price,typeid,typename)values(?,?,?,?,?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql,entity.getName(),entity.getNotes(),entity.getAuthor(),entity.getPublish(),entity.getImg(),entity.getState(),
                    entity.getIsbn(),entity.getPrice(),entity.getTypeid(),entity.getTypename());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateById(Book entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql ="update t_books set name = ?,notes = ? ,author = ? , publish = ?, img = ?,state = ?, isbn = ?,price = ?, typeid = ?, typename = ? where id = ?";
        try {
            return queryRunner.update(sql,entity.getName(),entity.getNotes(),entity.getAuthor(),entity.getPublish(),entity.getImg(),entity.getState(),
                    entity.getIsbn(),entity.getPrice(),entity.getTypeid(),entity.getTypename(),entity.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public Book findById(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_books where id =?";
        try {

            List<Book> list = queryRunner.query(sql, new BeanListHandler<Book>(Book.class), id);
            if (list !=null && list.size() > 0){
                return list.get(0);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }



    @Override
    public int deleteById(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql ="delete from t_books where id = ?";
        try {
            return queryRunner.update(sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
